package cc.linker.steplink.crm.merchant.service.impl;

import cc.linker.steplink.crm.merchant.dao.integral.IntegralGoodsDao;
import cc.linker.steplink.crm.merchant.dao.integral.IntegralGroupDao;
import cc.linker.steplink.crm.merchant.service.integral.IntegralGroupService;
import cc.linker.steplink.crm.response.GlobalException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IntegralGroupServiceImpl implements IntegralGroupService {

    @Autowired
    private IntegralGroupDao integralGroupDao;
    @Autowired
    private IntegralGoodsDao integralGoodsDao;

    @Override
    public List<Map> getList(String orgId) {
        return integralGroupDao.getList(orgId);
    }

    @Override
    public void save(Map<String, Object> param) {
        //判断重名
        Integer count = integralGroupDao.getNameCount(param);
        if (count > 0) {
            throw GlobalException.build("专区名称不能重复！");
        }
        if (StringUtils.isEmpty(param.get("id"))) {
            //新增
            //之前的排序加1，新增的要第一个
            integralGroupDao.addSort(param);
            integralGroupDao.insert(param);
        } else {
            //编辑
            integralGroupDao.update(param);
        }
    }

    @Override
    public void delete(Map<String, Object> param) {
        integralGroupDao.delete(param);
    }

    @Override
    public void updateStatus(Map<String, Object> param) {
        integralGroupDao.updateStatus(param);
    }

    @Override
    public void updateSort(Long id, Integer sort, Integer type, String orgId) {
        Integer oldSort = sort;
        Integer nowSort = null;
        //type 0 向下 1 向上
        Map<String, Object> param = new HashMap<>();
        param.put("sort", sort);
        param.put("type", type);
        param.put("orgId", orgId);
        //查询
        Map groupMap = integralGroupDao.getGroupBySort(param);
        if (groupMap == null) {
            throw GlobalException.build("无法移动");
        }
        nowSort = Integer.valueOf(groupMap.get("sort").toString());
        //  获取当前商品的normalSort
        integralGroupDao.updateSort(Long.valueOf(groupMap.get("id").toString()), oldSort);
        integralGroupDao.updateSort(id, nowSort);
    }

    @Override
    public Object getGoodsList(Long groupId, Integer pageIndex, Integer pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Map> goodsList = integralGroupDao.getGoodsList(groupId);
        for (Map goodsInfo : goodsList) {
            String quotaLevel = "";
            if (!StringUtils.isEmpty(goodsInfo.get("quotaLevel")) && !goodsInfo.get("quotaLevel").toString().equals("0")) {
                quotaLevel = "大于等于vip" + goodsInfo.get("quotaLevel").toString();
            }
            if (!StringUtils.isEmpty(goodsInfo.get("quotaVips"))) {
                String quotaVipNames1 = integralGoodsDao.getQuotaVipNames(goodsInfo.get("quotaVips").toString());
                if (!StringUtils.isEmpty(quotaLevel)) {
                    quotaLevel = quotaLevel + ";" + quotaVipNames1;
                } else {
                    quotaLevel = quotaVipNames1;
                }
            }
            goodsInfo.put("quotaVipNames", quotaLevel);
        }
        return new PageInfo<>(goodsList);
    }

    @Override
    public void addIntegralGoods(Map<String, Object> param) {
        List<Long> integralGoodsIds = (List<Long>) param.get("integralGoodsIds");
        Integer integralGoodsCount = 0;
        if (integralGoodsIds != null && integralGoodsIds.size() > 0) {
            integralGoodsCount = integralGoodsIds.size();
        }
        param.put("integralGoodsCount", integralGoodsCount);
        integralGroupDao.addIntegralGoods(param);
    }

    @Override
    public void deleteIntegralGoods(Map<String, Object> param) {
        integralGroupDao.deleteIntegralGoods(param);
    }

    @Override
    public void updateGoodsSort(Long groupId, Long integralGoodsId, Integer sort, Integer type) {
        Integer oldSort = sort;
        Integer nowSort = null;
        //type 0 向下 1 向上
        Map<String, Object> param = new HashMap<>();
        param.put("sort", sort);
        param.put("type", type);
        param.put("groupId", groupId);
        //查询
        Map godosMap = integralGroupDao.getGoodsBySort(param);
        if (godosMap == null) {
            throw GlobalException.build("无法移动");
        }
        nowSort = Integer.valueOf(godosMap.get("sort").toString());
        //  获取当前商品的normalSort
        integralGroupDao.updateGoodsSort(Long.valueOf(godosMap.get("integralGoodsId").toString()), groupId, oldSort);
        integralGroupDao.updateGoodsSort(integralGoodsId, groupId, nowSort);
    }
}
